﻿using SimpleChart.Kernel.Painter;
using SimpleChart.Kernel.Series;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SimpleChart.Kernel
{
    class ChartHelper
    {
        /// <summary>
        /// 获取axisYType指定的纵坐标应该表示的最大值（多个series要显示的值中的最大值）的天花板数（0.00000001乘以N次10））
        /// </summary>
        /// <param name="data"></param>
        /// <param name="seriesCollection"></param>
        /// <param name="axisYType"></param>
        /// <returns></returns>
        public static decimal GetAxisYCeiling(DataTable data, ICollection<SeriesBase> seriesCollection, AxisYType axisYType)
        {
            decimal maxValue = (decimal)GetMaxValue(data, seriesCollection, axisYType);

            decimal var = 0.0000000000000001m;
            while (var <= maxValue)
            {
                var *= 10;
            }

            return var;
        }

        private static float GetMaxValue(DataTable data, ICollection<SeriesBase> seriesCollection, AxisYType axisYType)
        {
            float maxValue = 0;
            foreach (var item in seriesCollection)
            {
                if (item.AxisType == axisYType)
                {
                    string valueMember = item.AxisYMember;
                    foreach (DataRow row in data.Rows)
                    {
                        float temp = float.Parse(row[valueMember].ToString());
                        if (temp > maxValue)
                        {
                            maxValue = temp;
                        }
                    }
                }
            }

            return maxValue;
        }

        /// <summary>
        /// 获取第一个参数data中需要绘制的点的坐标位置
        /// </summary>
        /// <param name="data"></param>
        /// <param name="floor"></param>
        /// <param name="ceiling"></param>
        /// <param name="axisYMember"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static List<PointF> GetPoints(DataTable data, float floor, float ceiling, string axisYMember, int width, int height)
        {
            float xDistance = (width - PainterParameters.MARGIN * 2 - PainterParameters.AXIS_Y_WIDHT * 2) / data.Rows.Count;
            float xPosition = PainterParameters.MARGIN + PainterParameters.AXIS_Y_WIDHT + xDistance / 2;

            List<PointF> points = new List<PointF>();
            foreach (DataRow row in data.Rows)
            {
                float value = float.Parse(row[axisYMember].ToString());
                float yPosition = GetYPosition(value, floor, ceiling, height);

                points.Add(new PointF { X = xPosition, Y = yPosition });

                xPosition += xDistance;
            }
            return points;
        }

        private static float GetYPosition(float value, float floor, float ceiling, int height)
        {
            float rate = value / (ceiling - floor);
            float sereisAreaHeight = height - PainterParameters.MARGIN * 2 - PainterParameters.LEGEND_HEIGHT - PainterParameters.AXIS_X_HEIGHT - PainterParameters.TITLE_HEIGHT;
            float floorPosition = height - PainterParameters.MARGIN - PainterParameters.LEGEND_HEIGHT - PainterParameters.AXIS_X_HEIGHT;
            float yPosition = floorPosition - sereisAreaHeight * rate;

            return yPosition;
        }
    }
}
